package com.example.yin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.yin.common.R;
import com.example.yin.mapper.SysLogMapper;
import com.example.yin.model.domain.Consumer;
import com.example.yin.model.domain.SysLog;
import com.example.yin.model.VTO.SysLogWithName;
import com.example.yin.service.SysLogService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import org.apache.catalina.connector.RequestFacade;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
* @author Payment
* @description 针对表【sys_log】的数据库操作Service实现
* @createDate 2023-07-10 09:37:48
*/
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog>
    implements SysLogService {
    @Autowired
    private SysLogMapper sysLogMapper;
    @Override
    public R loginInfo(JoinPoint jp, R result) {
        // 获取方法参数列表
        Object[] args=jp.getArgs();

        RequestFacade requestFacade=(RequestFacade)args[2];
        // 获取查询结果中的用户列表
        ArrayList<Consumer> consumers= (ArrayList<Consumer>) result.getData();
        // 插入登录日志到数据库
        if (sysLogMapper.insert(getloginInfo(consumers.get(0),requestFacade))>0) {
            return R.success("日志插入成功", consumers.get(0));
        } else {
            return R.error("日志插入失败");
        }
    }

    @Override
    public R allLog() {
        // 查询日志列表，并关联查询用户信息
        List<SysLogWithName> list = sysLogMapper.selectJoinList(SysLogWithName.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysLog.class)
                        .select(Consumer::getUsername)
                        .leftJoin(Consumer.class,Consumer::getId,SysLog::getUserid));
        // 返回成功响应，包含查询到的日志列表
        return R.success("查询成功",list);
    }

    private SysLog getloginInfo(Consumer consumer, RequestFacade request){
        // 获取User-Agent信息
        String agentStr = request.getHeader("user-agent");
        UserAgent agent = UserAgent.parseUserAgentString(agentStr);
        // 获取IP地址
        String ip = request.getHeader("x-forwarded-for");
        // 判断IP地址是否为空或未知
        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        // 判断IP地址是否为空或未知
        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        // 判断IP地址是否为空或未知
        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        // 判断IP地址是否为本地回环地址
        if(ip.equals("0:0:0:0:0:0:0:1")){
            ip="127.0.0.1";
        }
        // 获取操作系统信息和浏览器信息
        OperatingSystem os = agent.getOperatingSystem();
        Browser browser = agent.getBrowser();
        // 创建SysLog对象并设置相关信息
        SysLog sysLog=new SysLog();
        sysLog.setUserid(consumer.getId());
        sysLog.setLogContent("登陆成功");
        sysLog.setIpAddress(ip);
        sysLog.setOs(os.getName());
        sysLog.setIe(browser.getName());
        sysLog.setCreatedate(new Date());
        return sysLog;
    }
}




